﻿<!DOCTYPE html>
<html ng-app="demoApp" lang="en">
<head>
    <title id="Description">Rows Selection Modes in AngularJS DataTable. Select multiple rows by holding Shift or Ctrl and clicking on a row.</title>
      <meta name="description" content="This is an example of AngularJS DataTable. Rows Selection is demonstrated." />	
     <link rel="stylesheet" href="../../../jqwidgets/styles/jqx.base.css" type="text/css" />
    <script type="text/javascript" src="../../../scripts/angular.min.js"></script> 
    <script type="text/javascript" src="../../../scripts/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="../../../jqwidgets/jqxcore.js"></script>
    <script type="text/javascript" src="../../../jqwidgets/jqxdata.js"></script>
    <script type="text/javascript" src="../../../jqwidgets/jqxbuttons.js"></script>
    <script type="text/javascript" src="../../../jqwidgets/jqxscrollbar.js"></script>
    <script type="text/javascript" src="../../../jqwidgets/jqxdatatable.js"></script>
    <script type="text/javascript" src="../../../jqwidgets/jqxinput.js"></script>
    <script type="text/javascript" src="../../../jqwidgets/jqxlistbox.js"></script>
    <script type="text/javascript" src="../../../jqwidgets/jqxdropdownlist.js"></script>
    <script type="text/javascript" src="../../sampledata/generatedata.js"></script>
    <script type="text/javascript" src="../../../scripts/demos.js"></script>
    <script type="text/javascript" src="../../../jqwidgets/jqxangular.js"></script>
    <script type="text/javascript">
        var demoApp = angular.module("demoApp", ["jqwidgets"]);
        demoApp.controller("demoController", function ($scope) {
            var grid;
            $scope.selectedRows = "";
            $scope.value = 0;
            // create AngularJS DataTable.
            $scope.gridSettings =
            {
                width: 650,
                created: function (args) {
                    grid = args.instance;
                },
                source: new $.jqx.dataAdapter({
                    localData: generateData(10),
                    datatype: "array",
                    dataFields:
                    [
                        { name: 'firstname', type: 'string' },
                        { name: 'lastname', type: 'string' },
                        { name: 'productname', type: 'string' },
                        { name: 'available', type: 'bool' },
                        { name: 'date', type: 'date' },
                        { name: 'quantity', type: 'number' },
                        { name: 'price', type: 'number' }
                    ]
                }),
                altRows: true,
                selectionMode: "multipleRows",
                columns: [
                  { text: 'First Name', dataField: 'First Name', width: 200 },
                  { text: 'Last Name', dataField: 'Last Name', width: 200 },
                  { text: 'Product', dataField: 'Product', width: 180 },
                  { text: 'Unit Price', dataField: 'Price', width: 90, align: 'right', cellsAlign: 'right', cellsFormat: 'c2' },
                  { text: 'Quantity', dataField: 'Quantity', width: 80, align: 'right', cellsAlign: 'right' }
                ]
            };

            // create jqxDropDownList.
            $scope.selectionModeSettings = { autoDropDownHeight: true, selectedIndex: 1, source: ['Single Row', 'Multiple Rows'], height: 30 };
            $scope.selectionMode = function (event) {
                switch (event.args.index) {
                    case 0:
                        // disable multiple rows selection with Shift or Ctrl.
                        $scope.gridSettings.selectionMode = 'singleRow';
                        break;
                    case 1:
                        // enable multiple rows selection with Shift or Ctrl.
                        $scope.gridSettings.selectionMode = 'multipleRows';
                        break;
                }
            };

            // selects a row by index.
            $scope.rowSelectClick = function () {
                grid.selectRow(parseInt($scope.value));
            };

            // clear selection.
            $scope.clearSelectionClick = function () {
                grid.clearSelection();
            };

            var selectionInfo = function () {
                // gets selected row indexes. The method returns an Array of indexes.
                var selection = grid.getSelection();
                var selectedRows = "Selected Row Indexes:";
                if (selection && selection.length > 0) {
                    var rows = grid.getRows();
                    for (var i = 0; i < selection.length; i++) {
                        var rowData = selection[i];
                        selectedRows += rows.indexOf(rowData);
                        if (i < selection.length - 1) {
                            selectedRows += ", ";
                        }
                    }
                }
                $scope.selectedRows = selectedRows;
            }

            $scope.rowSelect = function (event) {
                // event arguments
                var args = event.args;
                // row index
                var index = args.index;
                // row data
                var rowData = args.row;
                // row key
                var rowKey = args.key;
                selectionInfo();
            };

            $scope.rowUnselect = function (event) {
                // event arguments
                var args = event.args;
                // row index
                var index = args.index;
                // row data
                var rowData = args.row;
                // row key
                var rowKey = args.key;
                selectionInfo();
            };
        });
    </script>
</head>
<body ng-controller="demoController">
    <div style="float: left;">
        <jqx-data-table jqx-watch="gridSettings.selectionMode" jqx-settings="gridSettings" jqx-on-row-select="rowSelect(event)" jqx-on-row-unselect="rowUnselect(event)">
           <table border="1">
            <thead>
                <tr>
                    <th align="left">First Name</th>
                    <th align="left">Last Name</th>
                    <th align="left">Product</th>
                    <th align="right">Price</th>
                    <th align="right">Quantity</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Ian</td>
                    <td>Devling</td>
                    <td>Espresso Truffle</td>
                    <td>$1.75</td>
                    <td>8</td>
                </tr>
                <tr>
                    <td>Nancy</td>
                    <td>Wilson</td>
                    <td>Cappuccino</td>
                    <td>$5.00</td>
                    <td>3</td>
                </tr>
                <tr>
                    <td>Cheryl</td>
                    <td>Nodier</td>
                    <td>Caffe Americano</td>
                    <td>$2.50</td>
                    <td>4</td>
                </tr>
                <tr>
                    <td>Martin</td>
                    <td>Saavedra</td>
                    <td>Caramel Latte</td>
                    <td>$3.80</td>
                    <td>11</td>
                </tr>
                <tr>
                    <td>Guylene</td>
                    <td>Bjorn</td>
                    <td>Green Tea</td>
                    <td>$1.50</td>
                    <td>8</td>
                </tr>
                <tr>
                    <td>Andrew</td>
                    <td>Burke</td>
                    <td>Caffe Espresso</td>
                    <td>$3.00</td>
                    <td>2</td>
                </tr>
                <tr>
                    <td>Regina</td>
                    <td>Murphy</td>
                    <td>White Chocolate Mocha</td>
                    <td>$3.60</td>
                    <td>6</td>
                </tr>
                <tr>
                    <td>Michael</td>
                    <td>Murphy</td>
                    <td>Caramel Latte</td>
                    <td>$3.80</td>
                    <td>2</td>
                </tr>
                <tr>
                    <td>Petra</td>
                    <td>Bein</td>
                    <td>Caffe Americano</td>
                    <td>$2.50</td>
                    <td>7</td>
                </tr>
                <tr>
                    <td>Nancy</td>
                    <td>Nodier</td>
                    <td>Caffe Latte</td>
                    <td>$4.50</td>
                    <td>10</td>
                </tr>
                <tr>
                    <td>Peter</td>
                    <td>Devling</td>
                    <td>Green Tea</td>
                    <td>$1.50</td>
                    <td>1</td>
                </tr>
                <tr>
                    <td>Beate</td>
                    <td>Saylor</td>
                    <td>Espresso con Panna</td>
                    <td>$3.25</td>
                    <td>3</td>
                </tr>
                <tr>
                    <td>Shelley</td>
                    <td>Nodier</td>
                    <td>Peppermint Mocha Twist</td>
                    <td>$4.00</td>
                    <td>7</td>
                </tr>
                <tr>
                    <td>Nancy</td>
                    <td>Murphy</td>
                    <td>Peppermint Mocha Twist</td>
                    <td>$4.00</td>
                    <td>1</td>
                </tr>
                <tr>
                    <td>Lars</td>
                    <td>Wilson</td>
                    <td>Caffe Espresso</td>
                    <td>$3.00</td>
                    <td>11</td>
                </tr>
            </tbody>
        </table>
        </jqx-data-table>
    </div>
    <div style="float: left; margin-left: 10px;">
        <div><strong>Settings</strong></div>
        Select Row:
        <jqx-input ng-model="value" jqx-width="60" jqx-height="30" jqx-on-change="rowInput(event)"></jqx-input>
        <br />
        <br />
        <jqx-button jqx-width="60" jqx-height="30" jqx-on-click="rowSelectClick()">Select</jqx-button>
        <jqx-button jqx-height="30" jqx-on-click="clearSelectionClick()">Clear Selection</jqx-button>
        <br />
        <br />
        {{selectedRows}}
        <br />
        Selection Mode:
         <jqx-drop-down-list jqx-on-change="selectionMode(event)" jqx-settings="selectionModeSettings"></jqx-drop-down-list>
    </div>
</body>
</html>
